/**
 * 用户路由
 */
const router = require('koa-router')()
const { getList, getDetail, register, editUser, deleteUser, login } = require('../controller/user')
const { SuccessModel, ErrorModel } = require('../model/resModel')
const { getCurrentTime } = require('../util/currentTime')

router.prefix('/api/user')

router.post('/register', async function (ctx, next) {
    const postData = ctx.request.body
    console.log('ctx.request.body', ctx.request.body)
    const username = postData.username
    const realname = postData.realname
    const password = postData.password
    const promiseValue = await register(username, password, realname, getCurrentTime(), getCurrentTime())
    if (promiseValue.insertId) {
        ctx.body = new SuccessModel({
            tip: '注册成功',
            createTime: getCurrentTime()
        }, '注册用户')
    } else {
        ctx.body = new ErrorModel({
            tip: '注册失败',
            failTime: getCurrentTime()
        }, '注册用户')
    }
})

router.get('/login', async function (ctx, next) {
    const username = ctx.query.username
    const password = ctx.query.password
    const promiseValue = await login(username, password)
    if (promiseValue.length > 0) {
        ctx.session.username = username
        ctx.session.password = password
        ctx.body = new SuccessModel({
            tip: '登录成功',
            loginTime: getCurrentTime()
        }, '用户登录')
    } else {
        ctx.body = new ErrorModel({
            tip: '登录失败',
            failTime: getCurrentTime()
        }, '用户登录')
    }
})

router.post('/login', async function (ctx, next) {
    const postData = ctx.request.body
    const username = postData.username
    const password = postData.password
    const promiseValue = await login(username, password)
    if (promiseValue.length > 0) {
        ctx.session.username = username
        ctx.session.password = password
        ctx.body = new SuccessModel({
            tip: '登录成功',
            loginTime: getCurrentTime()
        }, '用户登录')
    } else {
        ctx.body = new ErrorModel({
            tip: '登录失败',
            failTime: getCurrentTime()
        }, '用户登录')
    }
})

router.get('/login-test', async (ctx, next) => {
    if (ctx.session.username) {
        ctx.body = new SuccessModel({
            tip: '已登录',
            checkTime: getCurrentTime()
        }, '检测登录')
    } else {
        ctx.body = new ErrorModel({
            tip: '未登录',
            failTime: getCurrentTime()
        }, '检测登录')
    }
})

router.get('/list', async function (ctx, next) {
    const keyword = ctx.query.keyword
    const promiseValue = await getList(keyword)
    ctx.body = new SuccessModel(promiseValue, '用户列表')
})

router.get('/detail', async function (ctx, next) {
    const id = ctx.query.id
    const promiseValue = await getDetail(id)
    console.log('promiseValue', promiseValue)
    if (promiseValue.length > 0) {
        ctx.body = new SuccessModel(promiseValue[0], '用户详情')
    } else {
        ctx.body = new ErrorModel({
            tip: '查询失败，无此用户',
            failTime: getCurrentTime()
        }, '用户详情')
    }
})

router.post('/edit', async function (ctx, next) {
    const postData = ctx.request.body
    const username = postData.username
    const realname = postData.realname
    const password = postData.password
    const id = ctx.query.id
    const promiseValue = await editUser(username, password, realname, getCurrentTime(), id)
    if (promiseValue.affectedRows) {
        ctx.body = new SuccessModel({
            tip: '编辑成功',
            updateTime: getCurrentTime()
        }, '编辑用户')
    } else {
        ctx.body = new ErrorModel({
            tip: '编辑失败',
            failTime: getCurrentTime()
        }, '编辑用户')
    }
})

router.post('/delete', async function (ctx, next) {
    const id = ctx.query.id
    const promiseValue = await deleteUser(id)
    if (promiseValue.affectedRows) {
        ctx.body = new SuccessModel({
            tip: '删除成功',
            deleteTime: getCurrentTime()
        }, '删除用户')
    } else {
        ctx.body = new ErrorModel({
            tip: '删除失败',
            failTime: getCurrentTime()
        }, '删除用户')
    }
})

module.exports = router
